package com.fifth.apartment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fifth.apartment.entity.MVo;
import com.fifth.apartment.entity.Menu;
import com.fifth.apartment.entity.Meta;
import com.fifth.apartment.mapper.MenuMapper;
import com.fifth.apartment.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl< MenuMapper,Menu> implements MenuService {

    @Override
    public List<MVo> selectList(List<Menu> list) {
        return list.stream()
                .filter(menu -> menu.getParentId()==0)
                .peek(menu -> menu.setChildren(this.listChildMenu(menu.getId(),list)))
                .peek(menu -> {menu.setMeta(new Meta(menu.getIcon(),menu.getTitle()));})
                .map(menu ->new MVo(menu.getPath(),
                        menu.getName(),
                        menu.getRedirect(),
                        menu.getMeta(),
                        menu.getChildren()))
                .collect(Collectors.toList());
    }

    //递归查找子分类
    private List<MVo> listChildMenu(Integer id,List<Menu> menus){
        return menus.stream()
                .filter(menu1 -> menu1.getParentId() == id)
                .peek(m ->m.setChildren(this.listChildMenu(m.getId(),menus)))
                .peek(menu -> {menu.setMeta(new Meta(menu.getIcon(),menu.getTitle()));})
                .map(menu ->new MVo(menu.getPath(),
                        menu.getName(),
                        menu.getRedirect(),
                        menu.getMeta(),
                        menu.getChildren()))
                .collect(Collectors.toList());
    }
}
